package org.hzz.spring.code;

import lombok.extern.slf4j.Slf4j;
import org.hzz.spring.code.config.PkmerConfig;
import org.hzz.spring.code.context.BeanElement;
import org.hzz.spring.code.context.PkmerContext;
import org.hzz.spring.code.extensions.Pkmer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * @author 胖卡
 * @version 1.0.0
 * @date 2024/8/2
 */
@SpringBootApplication
@Slf4j
public class MarkMethodApp {
    public static void main(String[] args) {
        SpringApplication.run(MarkMethodApp.class,args);
    }


    @EventListener
    public void listen(ApplicationReadyEvent event){
        Map<String, BeanElement> map = PkmerContext.getMap();
        map.forEach((k,v) -> {
            log.info("开始执行：{}",k);
            Object target = v.getTarget();
            Method targetMethod = v.getTargetMethod();
            executeMethod(target,targetMethod);
        });
    }

    private void executeMethod(Object target,Method method){
        try {
            method.invoke(target);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}
